//
//  YeJianMoShi.m
//  SeeTheWorld
//
//  Created by dllo on 16/2/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YeJianMoShi.h"
#import <DKNightVersion.h>
#import "PCH.pch"

#import "CheckPhone.h"
#import <UIImageView+WebCache.h>


@interface YeJianMoShi ()

@property (nonatomic, retain)UIImageView *imageOfLeft;
@property (nonatomic, retain)UILabel *label;
@property (nonatomic, retain)UISwitch *sw;
@property (nonatomic, assign)NSUserDefaults *userDefaults;

@end

@implementation YeJianMoShi

- (void)dealloc {
    [_imageOfLeft release];
    [_label release];
    [_sw release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubviews];
        
    }
    return self;
}

- (void)createSubviews {
    
    [NSUserDefaults standardUserDefaults];
    
    self.imageOfLeft = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"night"]];
    [self.contentView addSubview:self.imageOfLeft];
    [_imageOfLeft release];
    
    self.label = [[UILabel alloc] init];
    [self.contentView addSubview:self.label];
    [_label release];
    self.label.text = @"夜间模式";
    self.label.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);

    
    self.sw = [[UISwitch alloc] init];
    [self.contentView addSubview:self.sw];
    
    /** 判断开关状态 */
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        self.sw.on = YES;
        
    } else {
        
        self.sw.on = NO;
    }
    
    [_sw release];
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    /** 判断手机型号 */
    NSString *iphone = [CheckPhone getCurrentDevice];
    if ([iphone isEqualToString:@"iPhone 6 Plus (A1522/A1524)"]) {
        
        self.imageOfLeft.frame = CGRectMake(25, 6, 30, 30);
        self.label.frame = CGRectMake(76, 6, 100, 30);
        
    } else {
        
        self.imageOfLeft.frame = CGRectMake(17, 6, 30, 30);
        self.label.frame = CGRectMake(62, 6, 100, 30);
    }
    
 
    self.sw.frame = CGRectMake(self.contentView.frame.size.width / 5 * 4 + 5, 6, 0, 0);
    [self.sw addTarget:self action:@selector(yeJianMoShi:) forControlEvents:UIControlEventValueChanged];
    
}

/** 开关事件 */  /** 0x343434原始颜色, 0xffffff黑色 */
- (void)yeJianMoShi:(UISwitch *)sw {
    
    if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNight) {
        
        /** 白天. */
        [DKNightVersionManager dawnComing];
        
        [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"夜间模式"];
        
    } else {
        
        /** 夜间. */
        [DKNightVersionManager nightFalling];
        
        [[NSUserDefaults standardUserDefaults] setBool:sw.on forKey:@"夜间模式"];
        
    }
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}









@end
